package com.shuwen.gcdj.controller.dictionary;


import com.shuwen.gcdj.bean.dictionaryItem.request.DictionaryItemAddParam;
import com.shuwen.gcdj.bean.dictionaryItem.request.DictionaryItemMainParam;
import com.shuwen.gcdj.bean.dictionaryItem.request.DictionaryItemUpdateParam;
import com.shuwen.gcdj.bean.dictionaryItem.response.DictionaryItemMainVO;
import com.shuwen.gcdj.common.annotation.Auth;
import com.shuwen.gcdj.common.annotation.JWTAuth;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.common.enums.ErrorCode;
import com.shuwen.gcdj.common.enums.LogModuleEnum;
import com.shuwen.gcdj.common.enums.LogTypeEnum;
import com.shuwen.gcdj.common.sysLog.AddSysLogUtil;
import com.shuwen.gcdj.controller.AuthController;
import com.shuwen.gcdj.dto.account.InitUserDto;
import com.shuwen.gcdj.service.dictionaryItem.DictionaryItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;


/**
 * @oauth psiyi
 * @createDate 2023/03/14
 * @info
 */
@Api(tags = "数据字典选项")
@Auth
@RestController
@RequestMapping("/dictionaryItem")
@Slf4j
public class DictionaryItemController extends AuthController {

    @Autowired
    private DictionaryItemService dictionaryItemService;

    @Autowired
    private AddSysLogUtil addSysLogUtil;

    @ApiOperation(value = "数据字典选项-列表")
    @PostMapping(value = "/list")
    @JWTAuth
    public CommonResult<List<DictionaryItemMainVO>> list(@Valid @RequestBody DictionaryItemMainParam param) {
        return dictionaryItemService.getList(param);
    }

    @ApiOperation(value = "数据字典选项-新增")
    @PostMapping(value = "/add")
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<DictionaryItemMainVO> add(@Valid @RequestBody DictionaryItemAddParam dictionaryItemAddParam, HttpServletRequest request) {
        InitUserDto initUserDto = initUser(request);
        CommonResult<DictionaryItemMainVO> add = dictionaryItemService.add(dictionaryItemAddParam, initUserDto);
        Long code = add.getCode();
        if (code.equals(ErrorCode.SUCCESS.getCode())){
            addSysLogUtil.addLog(initUserDto, LogTypeEnum.LOG_TYPE_ADD.getTypeNum(), LogModuleEnum.LOG_MODULE_DICTIONARY.getModuleNum(),"添加字典选项",dictionaryItemAddParam);
        }
        return add;
    }

    @ApiOperation(value = "数据字典选项-编辑")
    @PostMapping(value = "/update")
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<DictionaryItemMainVO> update(@Valid @RequestBody DictionaryItemUpdateParam dictionaryItemUpdateParam, HttpServletRequest request) {
        InitUserDto initUserDto = initUser(request);
        CommonResult<DictionaryItemMainVO> update = dictionaryItemService.update(dictionaryItemUpdateParam, initUserDto);
        Long code = update.getCode();
        if (code.equals(ErrorCode.SUCCESS.getCode())){
            addSysLogUtil.addLog(initUserDto, LogTypeEnum.LOG_TYPE_UPDATE.getTypeNum(), LogModuleEnum.LOG_MODULE_DICTIONARY.getModuleNum(),"编辑字典选项",dictionaryItemUpdateParam);
        }
        return update;
    }

    @ApiOperation(value = "数据字典选项-批量删除")
    @PostMapping(value = "/batchSave")
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public Object batchSave(@Valid @PathVariable String ids, HttpServletRequest request) {
        InitUserDto initUserDto = initUser(request);
        CommonResult<String> deletes = dictionaryItemService.deletes(ids, initUserDto);
        Long code = deletes.getCode();
        if (code.equals(ErrorCode.SUCCESS.getCode())){
            addSysLogUtil.addLog(initUserDto, LogTypeEnum.LOG_TYPE_DELETE.getTypeNum(), LogModuleEnum.LOG_MODULE_DICTIONARY.getModuleNum(),"删除字典选项",ids);
        }
        return deletes;
    }


}
